// pages/childoline/childonline.js
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true, //隐藏表单控件
    page: 1, //当前请求数据是第几页
    pageSize: 10, //每页数据条数
    hasMoreData: true, //上拉时是否继续请求数据，即是否还有更多数据
    contentlist: null,
    token: null,
    monitorexist: 0,
    end_time: null,
    show: false,
    timeList: null
  },
  btnclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var state = this.data.contentlist[index].state
    var service_state = this.data.contentlist[index].service_state;
    if(service_state==1){
    if (state == 1) {


      // var monitorid = this.data.contentlist[index].monitorid
      // var channelnumber = this.data.contentlist[index].channelnumber
      // var coverPhoto = this.data.contentlist[index].coverPhoto
      var timelist = this.data.contentlist[index].timelist
      app.globalData.zhiboUrl = this.data.contentlist[index].liveAddress
      var endtt = null
      for (var i = 0; i < timelist.length; i++) {
        var currentTime = util.formatTime1(new Date())
        var start = timelist[i].start
        var end = timelist[i].end
        if (currentTime >= start && currentTime < end) {
          endtt = end
        }
      }
      if (endtt != null) {
        this.setData({
          endtime1: endtt
        })
        wx.navigateTo({
          url: '../childonlinedetail/childonlinedetail?endtime=' + this.data.endtime1 + "&timelength=" + this.data.cameralength
        })
        // this.setData({
        //   monitorid: monitorid,
        //   channelnumber: channelnumber,
        //   coverPhoto: coverPhoto,
        //   endtime1: endtt
        // })
        // this.getToken(monitorid, channelnumber);
      } else {
        wx.showToast({
          title: "该设备不在直播时间段无法播放",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }

    } else if (state == 0) {
      wx.showToast({
        title: "该设备离线无法播放",
        icon: 'none',
        duration: 1000,
        mask: true
      })

    } else if (state == 2) {
      wx.showToast({
        title: "该设备不在直播时间段无法播放",
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
  }
  else if(service_state==-1){
    wx.showToast({
      title: "未开始",
      icon: 'none',
      duration: 2000,
      mask: true
    })
  }
    else if(service_state==-2){
      wx.showToast({
        title: "已结束",
        icon: 'none',
        duration: 2000,
        mask: true
      })
  }
  else if(service_state==-3){
    wx.showToast({
      title: "时间值在数据库中出现设置错误",
      icon: 'none',
      duration: 2000,
      mask: true
    })
  }

  },
  btntimeclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var timeList = this.data.contentlist[index].timelist

    this.setData({
      timeList: timeList,
      show: true
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    // 页面初始化 options为页面跳转所带来的参数
    this.data.page = 1
    this.getInfo('正在刷新数据')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},


  // 获取分页列表
  getInfo: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "version_interface": "2",
      "pagenum": this.data.page,
      "pagesize": that.data.pageSize,
      "mobileType": "1",
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_monitor_new180118"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          that.setData({
            cameralength: res.data.cameralength
          })
          var contentlist = res.data.data.monitortimeList;
          if (contentlist != null && contentlist.length > 0) {
            var monitorexist = res.data.data.monitorexist
            if (monitorexist == 0) {
              wx.showToast({
                title: "当前园所没有设置摄像头，请联系园所设置",
                icon: 'none',
                duration: 1000,
                mask: true
              })
            } else {

              // var payFlag=res.data.data.pay_flag
              // if(payFlag)          
              // var end_time = res.data.data.end_time
              // let value = new Date(end_time) - new Date();
              // let isActive = value > 0 ? true : false;
              // if (isActive) {
              that.setData({
                contentlist: contentlist,
                token: res.data.data.token,
                // endTime: res.data.data.end_time,
              })
              // }


            }

          }
        } else {
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          wx.hideNavigationBarLoading()
          wx.hideLoading()
        }

      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

  },





  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.data.page = 1
    this.getInfo('正在刷新数据')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  onClose() {
    this.setData({
      show: false
    });
  },
})